class Solution {
private:
    unordered_set<char> row[10];
    unordered_set<char> col[10];
public:
    bool isValid(int x,int y,char val,vector<vector<char>>& board){

        if(row[x].count(val) || col[y].count(val)){
            return false;
        }
        for(int i=(x/3)*3;i<(x/3)*3+3;++i){
            for(int j=(y/3)*3;j<(y/3)*3+3;++j){
                if(i==x && j==y){
                    continue;
                }
                if(board[i][j]==val){
                    return false;
                }
            }
        }
        
        return true;
    }
    bool isValidSudoku(vector<vector<char>>& board) {
        
        int n=board.size(),m=board[0].size();
        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j){
                if(board[i][j]=='.'){
                    continue;
                }
                if(!isValid(i,j,board[i][j],board)){
                    return false;
                }
                row[i].insert(board[i][j]);
                col[j].insert(board[i][j]);
            }
        }

        return true;
    }
};
